


package jnpf.zc.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.exception.DataException;
import jnpf.permission.entity.UserEntity;
import jnpf.util.GeneraterSwapUtil;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import jnpf.zc.entity.ZcglZcdjInfoEntity;
import jnpf.zc.entity.ZcrzZcrzEntity;
import jnpf.zc.mapper.ZcglZcdjInfoMapper;
import jnpf.zc.model.zcrzzcrz.ZcrzZcrzForm;
import jnpf.zc.model.zcrzzcrz.ZcrzZcrzInfoVO;
import jnpf.zc.model.zcrzzcrz.ZcrzZcrzListVO;
import jnpf.zc.model.zcrzzcrz.ZcrzZcrzPagination;
import jnpf.zc.service.ZcrzZcrzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 * ZcrzZcrz
 * @版本： V3.2.0
 * @版权： 智果云科技（https://www.jnpfsoft.com）
 * @作者： JNPF开发平台组
 * @日期： 2024-12-31
 */
@Slf4j
@RestController
@Api(tags = "ZcrzZcrz" , value = "example")
@RequestMapping("/api/example/ZcrzZcrz")
public class ZcrzZcrzController {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;


    @Autowired
    private UserProvider userProvider;


    @Autowired
    private ZcrzZcrzService zcrzZcrzService;

    @Autowired
    private ZcglZcdjInfoMapper zcdjInfoMapper;



    /**
     * 列表
     *
     * @param zcrzZcrzPagination
     * @return
     */
    @ApiOperation("获取列表")
	@PostMapping("/getList")
    public ActionResult list(@RequestBody ZcrzZcrzPagination zcrzZcrzPagination)throws IOException{
        List<ZcrzZcrzEntity> list= zcrzZcrzService.getList(zcrzZcrzPagination);
        //处理id字段转名称，若无需转或者为空可删除

    List<ZcrzZcrzListVO> listVO=JsonUtil.getJsonToList(list,ZcrzZcrzListVO.class);
    for(ZcrzZcrzListVO zcrzZcrzVO:listVO){
								Map<String,Object> assetNameMap = new HashMap<>();
//                zcrzZcrzVO.setAssetName_name(generaterSwapUtil.getPopupSelectValue("612769091110831365","id","fullName",zcrzZcrzVO.getAssetName(),assetNameMap));
//                zcrzZcrzVO.setAssetName_name(zcdjInfoMapper.selectById(zcrzZcrzVO.getD));
//
                zcrzZcrzVO.setFinancingStartDate_name(zcrzZcrzVO.getFinancingStartDate());

                zcrzZcrzVO.setFinancingEndDate_name(zcrzZcrzVO.getFinancingEndDate());

                zcrzZcrzVO.setFinancingAmount_name(zcrzZcrzVO.getFinancingAmount());

                zcrzZcrzVO.setFinancingContract_name(zcrzZcrzVO.getFinancingContract());

                zcrzZcrzVO.setFinancingPurpose_name(zcrzZcrzVO.getFinancingPurpose());

                zcrzZcrzVO.setCreditor_name(zcrzZcrzVO.getCreditor());

                zcrzZcrzVO.setDebtor_name(zcrzZcrzVO.getDebtor());

                zcrzZcrzVO.setNotes_name(zcrzZcrzVO.getNotes());

                zcrzZcrzVO.setFinancingFile_name(generaterSwapUtil.getFileNameInJson(zcrzZcrzVO.getFinancingFile()));

		//子表数据转换
     }

         PageListVO vo=new PageListVO();
         vo.setList(listVO);
         PaginationVO page=JsonUtil.getJsonToBean(zcrzZcrzPagination,PaginationVO.class);
         vo.setPagination(page);
         return ActionResult.success(vo);

    }

    /**
     * 创建
     *
     * @param zcrzZcrzForm
     * @return
     */
    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid ZcrzZcrzForm zcrzZcrzForm) throws DataException {
        boolean b =  zcrzZcrzService.checkForm(zcrzZcrzForm,0);
        if (b){
         return ActionResult.fail("单行输入不能重复");
        }
        String mainId =RandomUtil.uuId();

        UserInfo userInfo=userProvider.get();
		UserEntity userEntity = generaterSwapUtil.getUser(userInfo.getUserId());
        ZcrzZcrzEntity entity = JsonUtil.getJsonToBean(zcrzZcrzForm, ZcrzZcrzEntity.class);
        entity.setId(mainId);

        //根据资产编码获取资产名称
        ZcglZcdjInfoEntity zcglZcdjInfoEntity = zcdjInfoMapper.selectById(zcrzZcrzForm.getAssetName());
        entity.setAssetName(zcglZcdjInfoEntity.getZcName());

        zcrzZcrzService.save(entity);


        return ActionResult.success("创建成功");
}



    /**
    * 信息
    *
    * @param id
    * @return
    */
    @ApiOperation("信息")
    @GetMapping("/{id}")
    public ActionResult<ZcrzZcrzInfoVO> info(@PathVariable("id") String id){
        ZcrzZcrzEntity entity= zcrzZcrzService.getInfo(id);
        ZcrzZcrzInfoVO vo=JsonUtil.getJsonToBean(entity, ZcrzZcrzInfoVO.class);

    //子表
    //副表
        return ActionResult.success(vo);
    }

    /**
    * 表单信息(详情页)
    *
    * @param id
    * @return
    */
    @ApiOperation("表单信息(详情页)")
    @GetMapping("/detail/{id}")
    public ActionResult<ZcrzZcrzInfoVO> detailInfo(@PathVariable("id") String id){
        ZcrzZcrzEntity entity= zcrzZcrzService.getInfo(id);
        ZcrzZcrzInfoVO vo=JsonUtil.getJsonToBean(entity, ZcrzZcrzInfoVO.class);

 //子表数据转换

 //附表数据转换

//添加到详情表单对象中
								Map<String,Object> assetNameMap = new HashMap<>();
                vo.setAssetName(generaterSwapUtil.getPopupSelectValue("612769091110831365","id","fullName",vo.getAssetName(),assetNameMap));

                vo.setFinancingStartDate(vo.getFinancingStartDate());

                vo.setFinancingEndDate(vo.getFinancingEndDate());

                vo.setFinancingAmount(vo.getFinancingAmount());

                vo.setFinancingContract(vo.getFinancingContract());

                vo.setFinancingPurpose(vo.getFinancingPurpose());

                vo.setCreditor(vo.getCreditor());

                vo.setDebtor(vo.getDebtor());

                vo.setNotes(vo.getNotes());

                vo.setFinancingFile(generaterSwapUtil.getFileNameInJson(vo.getFinancingFile()));


        return ActionResult.success(vo);
		}




   /**
    * 更新
    *
    * @param id
    * @param zcrzZcrzForm
    * @return
    */
    @PutMapping("/{id}")
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id,@RequestBody @Valid ZcrzZcrzForm zcrzZcrzForm) throws DataException {
        boolean b =  zcrzZcrzService.checkForm(zcrzZcrzForm,1);
        if (b){
         return ActionResult.fail("单行输入不能重复");
        }
        UserInfo userInfo=userProvider.get();
        ZcrzZcrzEntity entity= zcrzZcrzService.getInfo(id);
        if(entity!=null){
            ZcrzZcrzEntity subentity=JsonUtil.getJsonToBean(zcrzZcrzForm, ZcrzZcrzEntity.class);
				boolean b1 = zcrzZcrzService.updateById(subentity);
				if (!b1){
				return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
				}
        return ActionResult.success("更新成功");
        }else{
            return ActionResult.fail("更新失败，数据不存在");
        }
    }



   /**
    * 删除
    *
    * @param id
    * @return
    */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id){
        ZcrzZcrzEntity entity= zcrzZcrzService.getInfo(id);
        if(entity!=null){
            zcrzZcrzService.delete(entity);

        }
        return ActionResult.success("删除成功");
    }






}
